﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CopyIt
{
    public partial class PairsForm : Form
    {
        //public event FormClosingEventHandler OnClosing;
        public bool IsNewPairs { get; private set; }

        private PairsParameters Params;
        public PairsForm(List<string> Include, List<string> Exclude, List<string> IncludeFold, List<string> ExcludeFold, bool isNew)
        {
            InitializeComponent();
            cbbInclude.Items.AddRange(Include.ToArray());
            cbbExclude.Items.AddRange(Exclude.ToArray());
            if (IncludeFold != null)
                cbbInclDir.Items.AddRange(IncludeFold.ToArray());
            if (ExcludeFold != null)
                cbbExclDir.Items.AddRange(ExcludeFold.ToArray());
            IsNewPairs = isNew;
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Params.PairsName.Equals(string.Empty))
                this.Text = "Modifica abbinamento " + Params.PairsName;
            else
            {
                this.Text = "Nuovo abbinamento ";
                Params.Guid = Guid.NewGuid().ToString();
            }
            base.OnLoad(e);
        }
        public PairsParameters PairsParameters
        {
            get
            {
                ReadParam();
                return Params;
            }
            set
            {
                Params = value;
                WriteParam(value);
            }
        }

        private void ReadParam()
        {
            Params.PairsName = txtPairs.Text;
            Params.SourceFolder = tfbSource.Text;
            Params.DestFolder = tfbDest.Text;
            Params.CurrentExcludeFilter = cbbExclude.Text;
            Params.CurrentIncludeFilter = cbbInclude.Text;
            Params.EnableExcludeFilter = ckbExclude.Checked;
            Params.EnableIncludeFilter = ckbInclude.Checked;
            Params.CurrentExclFolder = cbbExclDir.Text;
            Params.CurrentInclFolder = cbbInclDir.Text;
            Params.EnableExclFolder = ckbExlDir.Checked;
            Params.EnableInclFolder = ckbInclDir.Checked;
            Params.Override = (OverrideMode)cbbOverride.SelectedIndex;
            Params.Writemode = (WriteMode)cbbWriteMode.SelectedIndex;
            Params.ExcludeTypes =   (
                                        (ckbHidden.Checked ? System.IO.FileAttributes.Hidden : 0) |
                                        (ckbReadOnly.Checked ? System.IO.FileAttributes.ReadOnly : 0) |
                                        (ckbSystem.Checked ? System.IO.FileAttributes.System : 0)
                                    );
        }

        private void WriteParam(PairsParameters param)
        {
            txtPairs.Text = param.PairsName;
            tfbSource.Text = param.SourceFolder;
            tfbDest.Text = param.DestFolder;
            cbbExclude.Text = param.CurrentExcludeFilter;
            cbbInclude.Text = param.CurrentIncludeFilter;
            ckbExclude.Checked = param.EnableExcludeFilter;
            ckbInclude.Checked = param.EnableIncludeFilter;

            cbbExclDir.Text = Params.CurrentExclFolder;
            cbbInclDir.Text = Params.CurrentInclFolder;
            ckbExlDir.Checked = Params.EnableExclFolder;
            ckbInclDir.Checked = Params.EnableInclFolder;

            cbbOverride.SelectedIndex = (int)param.Override;
            cbbWriteMode.SelectedIndex = (int)param.Writemode;
            ckbReadOnly.Checked = (param.ExcludeTypes & System.IO.FileAttributes.ReadOnly) != 0;
            ckbHidden.Checked = (param.ExcludeTypes & System.IO.FileAttributes.Hidden) != 0;
            ckbSystem.Checked = (param.ExcludeTypes & System.IO.FileAttributes.System) != 0;
        }

        private void MyWizard_BeforeSwitchPages(object sender, Wizard.BeforeSwitchPagesEventArgs e)
        {
            WizardPage oldPage = this.MyWizard.Pages[e.OldIndex];
            if (oldPage == wzpSourceDest)
            {
                e.Cancel = CheckSourceDest();
                return;
            }
        }

        private bool CheckSourceDest()
        {
            if (tfbSource.Text.Equals(""))
            {
                MessageBox.Show("La cartella sorgente è vuota", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }

            if (tfbDest.Text.Equals(""))
            {
                MessageBox.Show("La cartella destinazione è vuota", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }
            if (tfbDest.Text.Contains(tfbSource.Text))
            {
                MessageBox.Show("La cartella destinazione è contenuta\nnella cartella di origine", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }

#if !DEBUG
            if (!Directory.Exists(tfbSource.Text))
            {
                MessageBox.Show("La cartella sorgente non esiste", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }

            if (!Directory.Exists(tfbDest.Text))
            {
                MessageBox.Show("La cartella destinazione non esiste", "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }
#endif
            return false;
        }

        private void ckbInclude_CheckedChanged(object sender, EventArgs e)
        {
            cbbInclude.Enabled = ckbInclude.Checked;
        }

        private void ckbExclude_CheckedChanged(object sender, EventArgs e)
        {
            cbbExclude.Enabled = ckbExclude.Checked;
        }

        private void MyWizard_Finish(object sender, EventArgs e)
        {

        }

        private void MyWizard_AfterSwitchPages(object sender, Wizard.AfterSwitchPagesEventArgs e)
        {
            WizardPage newPage = this.MyWizard.Pages[e.NewIndex];

            if (newPage == wzpName)
                MyWizard.CancelEnabled = !txtPairs.Text.Equals(string.Empty);
            else
                MyWizard.CancelEnabled = true;
        }

        private void txtPairs_TextChanged(object sender, EventArgs e)
        {
            MyWizard.CancelEnabled = !txtPairs.Text.Equals(string.Empty);
        }

        private void ckbInclDir_CheckedChanged(object sender, EventArgs e)
        {
            cbbInclDir.Enabled = ckbInclDir.Checked;
        }

        private void ckbExlDir_CheckedChanged(object sender, EventArgs e)
        {
            cbbExclDir.Enabled = ckbExlDir.Checked;
        }

        //private void PairsForm_FormClosing(object sender, FormClosingEventArgs e)
        //{
        //    if (OnClosing != null)
        //        OnClosing(sender, e);
        //}

    }
}
